package protocol

type PacketType uint8

const (
	RESERVED1   = 0
	CONNECT     = 1
	CONNACK     = 2
	PUBLISH     = 3
	PUBACK      = 4
	PUBREC      = 5
	PUBREL      = 6
	PUBCOMP     = 7
	SUBSCRIBE   = 8
	SUBACK      = 9
	UNSUBSCRIBE = 10
	UNSUBACK    = 11
	PINGREQ     = 12
	PINGRESP    = 13
	DISCONNECT  = 14
	RESSERVED2  = 15
)

func HeaderType(fixedHeader uint8) PacketType {
	ctlBits := (fixedHeader & 0xf0) >> 4
	return PacketType(ctlBits)
}

type FixedHeader struct {
	FirstLine byte
	RemainLen uint8
}

type BaseMessage struct {
	FixedHeader
	Data []byte
}

func (msg *BaseMessage) IsType(t PacketType) bool {
	ctlBits := (msg.FirstLine & 0xf0) >> 4
	return PacketType(ctlBits) == t
}

func (msg *BaseMessage) Type() PacketType {
	ctlBits := (msg.FirstLine & 0xf0) >> 4
	return PacketType(ctlBits)
}

type ConnectMsg struct {
	BaseMessage
}

type PublishMsg struct {
	BaseMessage
	Topic     string
	Identifer uint16
	Data      []byte
}

type PubBackMsg struct {
	BaseMessage
	Identifer uint16
}

type SubscribeMsg struct {
	BaseMessage
	Identifer       uint16
	SubscribeTopics []SubscribeTopic
}

type SubscribeTopic struct {
	Name string
	Ext  byte
}

type SubBackMsg struct {
	BaseMessage
	Identifer uint16
	RetCode   byte
}

func makePublishMsg() {

}
